import 'package:flutter/material.dart';
import 'package:flutter_module/animation/frames_animation.dart';
import 'package:flutter_module/base/model/constants.dart';
import 'package:flutter_module/base/util/sp_util.dart';
import 'package:flutter_module/controller/root_controller.dart';
import 'package:flutter_module/pages/root_page/four_page.dart';
import 'package:flutter_module/pages/root_page/home_page.dart';
import 'package:flutter_module/pages/root_page/second_page.dart';
import 'package:flutter_module/pages/root_page/third_page.dart';
import 'package:flutter_module/util/app_colors.dart';
import 'package:flutter_module/util/language_res/str_res_keys.dart';
import 'package:flutter_module/util/log_u.dart';
import 'package:get/get.dart';

class RootPage extends StatefulWidget {
  final Map<dynamic, dynamic> params;

  const RootPage({super.key, required this.params});

  @override
  _RootPageState createState() => _RootPageState();
}

class _RootPageState extends State<RootPage> {
  List<Widget> pages = [
    HomePage(),
    const SecondPage(),
    ThirdPage(),
    FourPage()
  ];

  //存放帧动画组件的key
  List<GlobalKey<FramesAnimationState>> frameKeys = [
    GlobalKey<FramesAnimationState>(debugLabel: 'home_key'),
    GlobalKey<FramesAnimationState>(debugLabel: 'second_key'),
    GlobalKey<FramesAnimationState>(debugLabel: 'third_key'),
    GlobalKey<FramesAnimationState>(debugLabel: 'four_key')
  ];

  late RootController _controller;

  @override
  void initState() {
    super.initState();

    SharedPreferencesU.preInit();
    var cookie = widget.params[Constants.cookie];
    //保存cookie，执行网络请求
    SharedPreferencesU.getInstance().setData(Constants.cookie, cookie);

    _controller = Get.find<RootController>();
    _controller.initList(pages, frameKeys,0,0);

    LogU.log("参数信息内容 ${_controller.currentIndex}");
  }

  @override
  Widget build(BuildContext context) {
    //屏幕适配
    // ScreenUtil.init(Get.context!, designSize: const Size(360, 690));

    return GetBuilder<RootController>(builder: (controller) {
      return Scaffold(
        body: PageView(
          controller: controller.pageController,
          physics: const NeverScrollableScrollPhysics(),
          children: controller.pages,
        ),
        bottomNavigationBar: Theme(
            //自定义主题样式
            data: ThemeData(
                splashColor: Colors.transparent,
                highlightColor: Colors.transparent),
            child: BottomNavigationBar(
              selectedItemColor: AppColors.color_3cceaf,
              unselectedItemColor: AppColors.color_383838,
              //选中和未选中状态的字号
              selectedFontSize: 10.0,
              unselectedFontSize: 10.0,
              currentIndex: controller.currentIndex,
              onTap: (int index) => controller.switchItem(index),
              type: BottomNavigationBarType.fixed,
              items: [
                BottomNavigationBarItem(
                    icon: controller.getFramesAnimation(
                        0, 'home_frames/home_frame'),
                    label: SR.home.tr),
                BottomNavigationBarItem(
                    icon: controller.getFramesAnimation(
                        1, 'category_frames/category_frame'),
                    label: SR.classify.tr),
                BottomNavigationBarItem(
                    icon: controller.getFramesAnimation(
                        2, 'cart_frames/cart_frame'),
                    label: SR.cart.tr),
                BottomNavigationBarItem(
                    icon: controller.getFramesAnimation(
                        3, 'mine_frames/mine_frame'),
                    label: SR.mine.tr),
              ],
            )),
      );
    });
  }
}
